import React from 'react'
import { changeNav } from '../../../actions'
import { connect } from 'react-redux'
import {
    Button, List, InputItem, Flex, WingBlank, WhiteSpace, Popup, Tag, Toast, Switch
} from 'antd-mobile'
import { createForm } from 'rc-form';
import hhistory from '../../../history'
import { ajaxGet, ajaxPost } from 'wxAjaxUtil';
import styles from './mdConfig.less';

const Item = List.Item;
const Brief = Item.Brief;


class WmConfig extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            res: {}
        };
        this.submit=this.submit.bind(this);
    }
    
    componentDidMount() {
        this.props.tochangeNav({
            title: "饿了吗外卖配置", rbtn: [
                <Button onClick={this.submit} key="1" type="primary" inline size="small">确定</Button>
            ]
        });
        
        var postData={
            "storeCode":this.props.wmConfigData.storeCode
        };
        console.log(postData);
        //请求数据
        ajaxPost("/o2o/sly/conf/load/ele/shop", postData, 'wxApiPath').then(res => {
            if (res.returnCode && res.returnCode == "1") {
                this.setState({
                    res: res.data,
                });
                this.state.res.storeInfo=postData.storeCode;
            } else {
                Toast.fail('加载饿了吗外卖配置失败', 1);
            }
        });
        
    }

    submit() {
        const subData = {
            "storeInfo":
            [
                this.state.res.storeInfo
            ],
            "username": this.state.res.username,
            "password": this.state.res.password,
            "riderFlg": this.state.res.riderFlg,
            "platFlg": this.state.res.platFlg,
            "shopId": this.state.res.shopId
        };
        ajaxPost("/o2o/sly/conf/apply/ele/shop", subData, 'wxApiPath').then(res => {
            if (res.returnCode && res.returnCode == "1") {
                Toast.success('应用饿了吗外卖配置成功', 1);
            } else {
                Toast.fail('应用饿了吗外卖配置失败', 1);
            }
        });
        var backRouter = this.props.location.state.backRouter;
        hhistory.push({
			pathname: backRouter
		});

    }

    render() {
        return (
            <div>
                <WhiteSpace size='md' styleName="yellow" />
                <List>
                    <Item><div><span>饿了吗商家账号</span><input onBlur={(e)=>{this.state.res.username=e.target.value}} className={styles.input} type="text" placeholder={this.state.res.username ? this.state.res.username : "必填"} /></div></Item>
                    <Item><span>饿了吗商家密码</span><input onBlur={(e)=>{this.state.res.password=e.target.value}} className={styles.input} type="text" placeholder={this.state.res.password ? this.state.res.password : "必填"} /></Item>
                    <Item><span>饿了吗店铺号</span><input onBlur={(e)=>{this.state.res.shopId=e.target.value}} className={styles.input} type="text" placeholder={this.state.res.shopId ? this.state.res.shopId : "必填"} /></Item>
                </List>
                <WhiteSpace size='md' />
                <WhiteSpace size='md' />
                <List>
                    <List.Item extra={<Switch checked={this.state.res.platFlg} onClick={() => { this.state.res.platFlg = this.state.res.platFlg?0:1; this.setState({ res: this.state.res }) }} />}>
                        平台配送</List.Item>
                    <List.Item extra={<Switch checked={this.state.res.riderFlg} onClick={() => { this.state.res.riderFlg = this.state.res.riderFlg?0:1; this.setState({ res: this.state.res }) }} />}>
                        第三方骑手接单状态</List.Item>
                </List>

            </div>
        )
    }


}

const mapStateToProps = (state) => ({
    wmConfigData: state.wxElms.wmConfigData
})
const mapDispatchToProps = (dispatch) => ({
    tochangeNav: navogj => dispatch(changeNav(navogj)),
})

export default connect(mapStateToProps, mapDispatchToProps)(WmConfig)